<?php
class BackendAdmin extends BaseDBModel {

    public function __construct()
    {
        $this->tableName = 'admin';
    }

    /**
     * @param string $email
     * @param string $password
     * @return array
     */
    public function isExists($email, $password)
    {
        return $this->getOne(
            "email=:email AND LOWER(passhash)=:passhash AND status=0",
            array(':email'=>$email,':passhash'=>strtolower(sha1(md5(md5($password)))))
        );
    }

    /**
     * @param integer $id
     * @return integer
     */
    public function lock($id)
    {
        $arr_admin = $this->getAdminOne($id);
        foreach ($arr_admin as $val) {
            if ($val['status'] == 1) {
                $stat = 0;
            } else {
                $stat = 1;
            }
        }
        return $this->update(array('status' => $stat), 'id=:id', array(':id'=>$id));
    }

    /**
     * @return array
     */
    public function getAdminGroupList()
    {
        $sql = "SELECT
                  t3.name,
                  GROUP_CONCAT(t2.description ORDER BY t2.description SEPARATOR ';<br>' ) AS description
                FROM admin_role_child AS t1
                LEFT JOIN admin_role AS t2 ON (t1.child=t2.NAME)
                LEFT JOIN admin_role AS t3 ON (t1.parent=t3.NAME)
                WHERE t3.name!='' AND t1.parent!=''
                GROUP BY t3.name";
        return $this->getCommand($sql)->query();
    }

    /**
     * @return array
     */
    public function getAdminList()
    {
        $sql = "SELECT *
                FROM admin AS t1
                LEFT JOIN admin_role_assignment AS t2 ON (t2.userid=t1.id)
                ORDER BY t1.id";
        return $this->getCommand($sql)->query();
    }

    /**
     * @param integer $id
     * @return array
     */
    public function getAdminOne($id)
    {
        $sql = "SELECT t1.id, t1.email, t1.status, t2.itemname
                FROM admin AS t1
                LEFT JOIN admin_role_assignment AS t2 ON (t1.id=t2.userid)
                WHERE t1.id=:id
                LIMIT 1";
        $command = $this->getCommand($sql);
        $command->bindParam(":id", $id, PDO::PARAM_INT);
        return $command->query();
    }

    /**
     * @param string $email
     * @param string $password
     * @param string $role
     * @param string $old_role
     * @param integer $id
     * @return void()
     */
    public function edit($email, $password, $role, $old_role, $id)
    {
        $this->update(array('email' => strtolower($email), 'passhash' => strtolower(sha1(md5(md5($password))))), 'id=:id', array(':id'=>$id));
        Yii::app()->authManager->revoke($old_role, $id);
        return Yii::app()->authManager->assign($role, $id);
    }

    /**
     * @param string $email
     * @param string $password
     * @param string $role
     * @return void
     */
    public function add($email, $password, $role)
    {
        $this->insert(array('email' => strtolower($email), 'passhash' => strtolower(sha1(md5(md5($password))))));
        return Yii::app()->authManager->assign($role, $this->getLastInsertID());
    }

    /**
     * @param integer $id
     * @param string $role
     * @return void
     */
    public function del($id, $role)
    {
        $this->delete('id=:id', array(':id' => $id));
        return Yii::app()->authManager->revoke(strtolower($role), $id);
    }

    /**
     * @return array
     */
    public function getRole()
    {
        $sql = "SELECT *
                FROM admin_role
                WHERE type=2";
        return $this->getCommand($sql)->queryAll();
    }

    /**
     * @return array
     */
    public function getChild()
    {
        $sql = "SELECT *
                FROM admin_role
                WHERE type=0
                ORDER by name";
        return $this->getCommand($sql)->queryAll();
    }

    /**
     * @param string $role
     * @return array
     */
    public function getChildByRole($role)
    {
        $sql = "SELECT t1.parent,t1.child
                FROM admin_role_child as t1
                LEFT JOIN admin_role AS t2 ON (t2.name=t1.parent)
                WHERE t1.parent=:role";
        $command = $this->getCommand($sql);
        $role = strtolower($role);
        $command->bindParam(":role", $role, PDO::PARAM_STR);
        return $command->query();
    }
}